package com.hbk.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hbk.base.common.CommonResult;
import com.hbk.base.common.Ref;
import com.hbk.base.util.TimeUtil;
import com.hbk.entity.Carousel;
import com.hbk.mapper.CarouselMapper;
import com.hbk.param.CarParam;
import com.hbk.service.ICacheService;
import com.hbk.service.ICarouselService;
import com.hbk.service.IOssService;
import com.hbk.vo.CarVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CarouselServiceImpl implements ICarouselService {

    @Resource
    private CarouselMapper carouselMapper;
    @Resource
    private IOssService ossService;
    @Resource
    private ICacheService cacheService;

    /**新增*/
    public CommonResult add(CarParam.AddParam param){
        Carousel carousel = new Carousel();
        carousel.setImgUrl(param.getImgUrl());
        carousel.setTitle(param.getTitle());
        carousel.setHasLink(param.getHasLink());
        if(param.getHasLink()==1){
            carousel.setLinkUrl(param.getLinkUrl());
        }else{
            carousel.setLinkUrl("");
        }
        carousel.setOrderNum(param.getOrderNum());
        Date date = new Date();
        carousel.setCreateTime(date);
        carousel.setUpdateTime(date);
        carouselMapper.insert(carousel);
        cacheService.deleteIndexCache();
        return Ref.ok();
    }

    /**修改*/
    public CommonResult modify(CarParam.ModifyParam param){
        Carousel carousel = carouselMapper.selectById(param.getId());
        if(carousel==null){
            return Ref.error("找不到轮播图");
        }
        carousel.setImgUrl(param.getImgUrl());
        carousel.setTitle(param.getTitle());
        carousel.setHasLink(param.getHasLink());
        if(param.getHasLink()==1){
            carousel.setLinkUrl(param.getLinkUrl());
        }else{
            carousel.setLinkUrl("");
        }
        carousel.setOrderNum(param.getOrderNum());
        carousel.setUpdateTime(new Date());
        carouselMapper.updateById(carousel);
        cacheService.deleteIndexCache();
        return Ref.ok();
    }

    /**获取所有*/
    public CommonResult listAll(){

        List<Carousel> carouselList = carouselMapper.listAll();
        List<CarVo.ListAllVo> vos = new ArrayList<>();
        for(int i=0; i<carouselList.size(); i++){
            Carousel car = carouselList.get(i);
            CarVo.ListAllVo vo = new CarVo.ListAllVo();
            vo.setId(car.getId());
            vo.setHasLink(car.getHasLink());
            vo.setImgUrl(car.getImgUrl());
            vo.setLinkUrl(car.getLinkUrl());
            vo.setOrderNum(car.getOrderNum());
            vo.setTitle(car.getTitle());
            vo.setCreateTime(TimeUtil.formaterYYYYMMddHHmm(car.getCreateTime()));
            vo.setUpdateTime(TimeUtil.formaterYYYYMMddHHmm(car.getUpdateTime()));
            vo.setIndexNum(i+1);
            vos.add(vo);
        }

        JSONArray array = new JSONArray();
        JSONObject json = null;
        for(int i=0; i<vos.size(); i++){
            if(i%4==0){
                json = new JSONObject();
                json.put("id", i);
                json.put("data", new JSONArray());
                array.add(json);
            }
            JSONArray data = json.getJSONArray("data");
            data.add(vos.get(i));
        }
        return Ref.ok(array);
    }

    /**删除*/
    public CommonResult delete(CarParam.DeleteParam param){
        Carousel carousel = carouselMapper.selectById(param.getId());
        if(carousel==null){
            return Ref.ok();
        }
        String imgUrl = carousel.getImgUrl();
        ossService.delete(imgUrl);
        carouselMapper.deleteById(param.getId());
        cacheService.deleteIndexCache();
        return Ref.ok();
    }

}
